﻿using PIMS.DBModel;
using PIMS.Interface;
using PIMS.WorkPlugin.ORM;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PIMS.WorkPlugin
{
    public partial class Form产品型号管理 : Form
    {
        private IDebugMsg debug = null;
        private ILog4net log = null;

        public Form产品型号管理()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;
        }

        private void btn添加新产品_Click(object sender, EventArgs e)
        {
            Form产品型号编辑 fmchangpw = new Form产品型号编辑();
            fmchangpw.ShowDialog();
            fmchangpw.Close();

            btnModIdRefresh_Click(null, null);
        }

        private void btnModIdChange_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 1)
            {
                MessageBox.Show("请选择要编辑的行后再试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            Form产品型号编辑 fmchangpw = new Form产品型号编辑((int)this.dataGridView1.SelectedRows[0].Cells["ID"].Value);
            fmchangpw.ShowDialog();
            fmchangpw.Close();

            btnModIdRefresh_Click(null,null);
        }

        private void btnModIdRefresh_Click(object sender, EventArgs e)
        {
            this.btnModIdRefresh.Enabled = false;
            new Thread(new ThreadStart(ThreadRefreshTypeTable)) { IsBackground = true }.Start();
        }

        private void ThreadRefreshTypeTable()
        {
            try
            {
                string cmdSql = "select * from 产品型号定义表";
                //DataTable dt = DbHelperSQL.Queryy(cmdSql);
                DataTable dt = SqlSugarHelper.QueryToDataTable<ProductType>(cmdSql);

                this.Invoke(
                new Action(() =>
                {
                    this.dataGridView1.DataSource = dt;
                }));
            }
            catch (Exception ex)
            {
                this.debug.DebugMsg(ex.Message,InfoLevel.error);
            }
            this.Invoke(
             new Action(() =>
             {
                 this.btnModIdRefresh.Enabled = true;
                 this.btnModIdChange.Enabled = true;
             }));
        }

        private void Form产品型号管理_Load(object sender, EventArgs e)
        {
            DgvTools.dataGridView(this.dataGridView1);
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this.btnModIdRefresh.Enabled = false;
            new Thread(new ThreadStart(ThreadRefreshTypeTable)) { IsBackground = true }.Start();
        }
    }

    [SugarTable("产品型号定义表")]
    public class ProductType
    {
        [SugarColumn(IsPrimaryKey = true,IsIdentity = true)]
        public int ID { get; set; }

        [SugarColumn(IsNullable = false, ColumnName = "型号", ColumnDescription = "型号")]
        public string name { get; set; }

        [SugarColumn(IsNullable = false, ColumnName = "物料编码", ColumnDescription = "物料编码")]
        public string code { get; set; }

        [SugarColumn(IsNullable = false, ColumnName = "粒径", ColumnDescription = "粒径")]
        public string size { get; set; }

        [SugarColumn(IsNullable = false, ColumnName = "模板ID", ColumnDescription = "模板ID")]
        public int modeId { get; set; }

        [SugarColumn(IsNullable = true, ColumnName = "备注", ColumnDescription = "备注")]
        public string mark { get; set; }

        [SugarColumn(IsNullable = true, ColumnName = "使能", ColumnDescription = "使能")]
        public string enable { get; set; }

        [SugarColumn(IsNullable = true, ColumnName = "时间", ColumnDescription = "时间")]
        public DateTime time { get; set; }
    }
}
